package com.sunday.aomicaichang.adpter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.model.Coupon;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 * Created by yun_sheng on 2017/6/4.
 */

public class CouponAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<Coupon> coupons;
    private Activity mActivity;
    private String amount;

    public CouponAdapter(Context mContext,Activity mActivity,String amount) {
        this.mContext = mContext;
        this.mActivity = mActivity;
        this.amount=amount;
    }

    public void setmData(List<Coupon> coupons) {
        this.coupons = coupons;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_coupon, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Coupon coupon = coupons.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if(coupon.getStatus()==2){
            viewHolder.lyCoupon.setEnabled(false);
            viewHolder.img.setVisibility(View.VISIBLE);
        }else {
            viewHolder.lyCoupon.setEnabled(true);
            viewHolder.img.setVisibility(View.GONE);
        }
        if (coupon.getDecuAmount() != null)
            viewHolder.money.setText(String.valueOf(coupon.getDecuAmount()));
        if (coupon.getBaseAmount() != null)
            viewHolder.name.setText("订单满"+coupon.getBaseAmount()+"元可用");
        if (coupon.getSource() != null)
            viewHolder.origin.setText(coupon.getSource());
        if (coupon.getEndTime() != null)
            viewHolder.time.setText(coupon.getEndTime());
        if(amount!=null)
        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data = new Intent();
                data.putExtra("coupon", coupon);
                mActivity.setResult(RESULT_OK, data);
                mActivity.finish();
            }
        });
    }

    @Override
    public int getItemCount() {
        return coupons == null ? 0 : coupons.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item)
        RelativeLayout item;
        @Bind(R.id.money_unit)
        TextView moneyUnit;
        @Bind(R.id.money)
        TextView money;
        @Bind(R.id.tip)
        TextView tip;
        @Bind(R.id.ly_ad_coupon)
        RelativeLayout lyAdCoupon;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.origin)
        TextView origin;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.ly_coupon)
        LinearLayout lyCoupon;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
